/**
 * 
 */
package org.t2.backstage.module.gameserver;

import java.util.List;

import org.gaming.backstage.PageData;
import org.gaming.backstage.PageQuery;
import org.gaming.backstage.interceptor.RoleContext;
import org.gaming.backstage.module.user.service.UserService;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.service.asserts.Asserts;
import org.gaming.tool.PageUtil;
import org.gaming.tool.SortUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.TipsCode;
import org.t2.backstage.module.gameserver.from.GameChannelForm;
import org.t2.backstage.module.gameserver.model.Bulletin;
import org.t2.backstage.module.gameserver.model.GameChannel;
import org.t2.backstage.module.gameserver.model.GameChannelServer;
import org.t2.backstage.module.gameserver.model.GamePlatform;
import org.t2.backstage.module.gameserver.model.GameRegion;
import org.t2.backstage.module.gameserver.service.BulletinService;
import org.t2.backstage.module.gameserver.service.GameChannelServerService;
import org.t2.backstage.module.gameserver.service.GameChannelService;
import org.t2.backstage.module.gameserver.service.GamePlatformService;
import org.t2.backstage.module.gameserver.service.GameRegionService;

/**
 * @author YY
 *
 */
@Controller
public class GameChannelController {
	
	@Autowired
	private GameChannelService gameChannelService;
	@Autowired
	private GameChannelServerService gameChannelServerService;
	@Autowired
	private UserService userService;
	@Autowired
	private BulletinService bulletinService;
	@Autowired
	private GamePlatformService gamePlatformService;
	@Autowired
	private GameRegionService gameRegionService;
	
	@ApiDocument("请求渠道数据")
	@RequestMapping(value = "/gameChannel/data.auth")
	@ResponseBody
	public PageData<GameChannel> info(PageQuery query) {
		List<GameChannel> list = gameChannelService.getEntityList(0);
		SortUtil.sort(list, GameChannel::getId);
		List<GameChannel> pageList = PageUtil.pageN(list, query.getPage(), query.getLimit());
		
		PageData<GameChannel> pageData = new PageData<>();
		pageData.setCount(list.size());
		pageData.setData(pageList);
		return pageData;
	}
	
	@ApiDocument("保存渠道数据，数据ID小于等于0时插入数据，大于0时更新对应数据")
	@RequestMapping(value = "/gameChannel/save.authw")
	@ResponseBody
	public GameChannel save(GameChannelForm form) {
		Bulletin bulletin = null;
		if(form.getBulletinId() > 0) {
			bulletin = bulletinService.getEntity(0, form.getBulletinId());
		}
		
		GameChannel gameChannel = null;
		if(form.getId() <= 0) {
			GamePlatform gamePlatform = gamePlatformService.getEntity(0, form.getPlatformId());
			Asserts.isTrue(gamePlatform != null, TipsCode.PLATFORM_MISS, form.getPlatformId());
			
			RoleContext roleContext = userService.getCurrUser();
			
			gameChannel = new GameChannel();
			gameChannel.setName(form.getName());
			gameChannel.setUserId(roleContext.getId());
			gameChannel.setUserName(roleContext.getName());
			gameChannel.setBulletinId(bulletin == null ? 0 : bulletin.getId());
			gameChannel.setPlatformId(gamePlatform.getId());
			gameChannel.setPlatformName(gamePlatform.getName());
			gameChannelService.insert(gameChannel);
		} else {
			gameChannel = gameChannelService.getEntity(0, form.getId());
			Asserts.isTrue(gameChannel != null, TipsCode.CHANNEL_MISS, form.getId());
			
			String oldName = gameChannel.getName();
			gameChannel.setName(form.getName());
			gameChannel.setBulletinId(bulletin == null ? 0 : bulletin.getId());
			gameChannelService.update(gameChannel);
			
			if(oldName != null && form.getName() != null && !oldName.equals(form.getName())) {
				List<GameChannelServer> list = gameChannelServerService.getEntityList(gameChannel.getId());
				for(GameChannelServer entity : list) {
					entity.setChannelName(gameChannel.getName());
				}
				gameChannelServerService.updateAll(list);
				
				List<GameRegion> regions = gameRegionService.getEntityList(gameChannel.getId());
				for(GameRegion gameRegion : regions) {
					gameRegion.setChannelName(gameChannel.getName());
				}
				gameRegionService.updateAll(regions);
			}
		}
		return gameChannel;
	}
	
	@ApiDocument("删除渠道数据，并删除与之关联的服务器关系数据")
	@RequestMapping(value = "/gameChannel/delete.authw")
	@ResponseBody
	public void delete(@RequestParam("channelId") long channelId) {
		GameChannel gameChannel = gameChannelService.getEntity(0, channelId);
		Asserts.isTrue(gameChannel != null, TipsCode.CHANNEL_MISS, channelId);
		
		gameChannelService.delete(gameChannel);
		List<GameChannelServer> relations = gameChannelServerService.getEntityList(gameChannel.getId());
		gameChannelServerService.deleteAll(relations);
	}
}
